package com.we;

import com.we.netty.AppClient;
import io.netty.buffer.*;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Nettytest {

    @Test
    public void testCompositeByteBuf(){
        //Unpolled是什么？
        //netty专门用来操作缓冲区的类
        ByteBuf header= Unpooled.buffer();
        ByteBuf body= Unpooled.buffer();
        //通过逻辑组装,而不是物理组装，也就是地址共享实现零拷贝
        CompositeByteBuf compositeByteBuf=Unpooled.compositeBuffer();
        compositeByteBuf.addComponents(header,body);
    }

    /**
     * 测试请求报文
     * @throws IOException
     */
    @Test
    public void testMessage() throws IOException {
        ByteBuf byteBuf=Unpooled.buffer();
        //魔术值 magic
        byteBuf.writeBytes("wcf".getBytes(StandardCharsets.UTF_8));
        //版本
        byteBuf.writeByte(1);
        //head len
        byteBuf.writeShort(125);
        //full length
        byteBuf.writeInt(256);

        byteBuf.writeByte(1);
        byteBuf.writeByte(0);
        byteBuf.writeByte(2);
        byteBuf.writeLong(251255L);
        //用对象流转换为字节数组
        AppClient appClient=new AppClient();

        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(appClient);
        byte[] bytes = outputStream.toByteArray();
        byteBuf.writeBytes(bytes);
        //将字节输出，会输出数字，转成二进制就是字节了
        printAsBinary(byteBuf);
    }

    public static void printAsBinary(ByteBuf byteBuf) {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(byteBuf.readerIndex(), bytes);

        String binaryString = ByteBufUtil.hexDump(bytes);
        StringBuilder formattedBinary = new StringBuilder();

        for (int i = 0; i < binaryString.length(); i += 2) {
            formattedBinary.append(binaryString.substring(i, i + 2)).append(" ");
        }

        System.out.println("Binary representation: " + formattedBinary.toString());
    }

    /**
     * 测试压缩
     */
    @Test
    public void testCompress() throws IOException {
        byte[] buf=new byte[]{12,12,12,12,12,23,45,67,89,12,12,12,12,12,23,45,67,89,12,12,12,12,12,23,45,67,89,12,12,12,12,12,23,45,67,89};
        //压缩本质就是将buf作为输入，将结果作为输出，输出到另一个字节数组中
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream=new GZIPOutputStream(baos);
        gzipOutputStream.write(buf);
        gzipOutputStream.finish();
        byte[] bytes = baos.toByteArray();
        //长度变化
        System.out.println(buf.length+"---->"+ bytes.length);
        System.out.println(Arrays.toString(bytes));
    }
    /**
     * 测试解压缩
     */
    @Test
    public void testDeCompress() throws IOException {
        byte[] buf=new byte[]{31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -29, -31, 1, 2, 113, 93, -25, 72, 30, -36, 12, 0, -75, 29, 63, -2, 36, 0, 0, 0};
        //压缩本质就是将buf作为输入，将结果作为输出，输出到另一个字节数组中
        ByteArrayInputStream bais=new ByteArrayInputStream(buf);
        GZIPInputStream gzipInputStream=new GZIPInputStream(bais);
        byte[] bytes = gzipInputStream.readAllBytes();
        //长度变化
        System.out.println(buf.length+"---->"+ bytes.length);
        System.out.println(Arrays.toString(bytes));
    }


}
